﻿<style>
    .weekly-item {
        cursor: pointer;
    }
</style>
<div>
    <table style="width: 100%;">
        <tr>
            <td style="width: 100%;">
                <textarea name="desc" id="weekly-content" style="min-height: 40px;padding: 0px;height: 42px;"
                          placeholder="请输入周报内容" class="layui-textarea"></textarea>
            </td>
            <td>
                <button class="layui-btn layui-btn-warm layui-btn-sm" id="weekly-btn" style="height: 42px;">发布
                </button>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="text" class="layui-input" id="weeklyTime"
                       name="time"
                       placeholder="yyyy-MM-dd" value="">
            </td>
        </tr>
    </table>
    <div id="table-weekly" style="overflow: auto"></div>
</div>
<!-- layui规范化用法 -->
<script type="text/javascript">
    var table = null;
    var toUserId = 0;
    var weeklyId = 0;
    var layer;

    $(document).ready(function () {
        $("#table-weekly").height(window.innerHeight - 200);
    });

    /*绑定下载按钮事件*/
    $(document).on("keyup", "#weekly-content", function () {
        var val = $("#weekly-content").val();
        if (val.length == 0) {
            toUserId = 0;
            dailyId = 0;
        }
    });
    /*绑定下载按钮事件*/
    workUtils.onclick(".weekly-item", function (c) {
        weeklyId = $(c).attr("val1");
        toUserId = $(c).attr("val2");
        $("#weekly-content").val('@' + $(c).attr("val3") + ' ');
    });
    /*发表评论*/
    workUtils.onclick("#weekly-btn", function () {
        var data = {
            toUserId: toUserId,
            weeklyId: weeklyId,
            projectId: workUtils.get("projectId"),
            content: $("#weekly-content").val()
        };
        if (data.content.length == 0) {
            workUtils.msg("内容不能为空");
            return;
        }
        var url = "../report/addWeekly";
        if (toUserId != 0) {
            url = "../report/addWeeklyItem";
        }
        var index = layer.load(1);
        workUtils.ajaxPost(url, data, function (result) {
            loadDefaultData();
            layer.close(index);
            $("#weekly-content").val("");
            weeklyId = 0;
            toUserId = 0;
        });
    });

    /*刷新任务*/
    function loadDefaultData() {
        workUtils.ajaxGet("../report/listWeekly", {
            projectId: workUtils.get("projectId"),
            week: $("#weeklyTime").val()
        }, function (data) {
            $("#table-weekly").html("");
            for (var i = 0; i < data.length; i++) {
                var html = '';
                var item = data[i];
                html += '<table style="width: 100%;">';
                html += '<td style="padding: 5px;text-align: center;vertical-align: top;" class="weekly-item" val1="' + item.id + '" val2="' + item.user.id + '" val3="' + item.user.name + '">';
                html += '<img src="../photo/get/' + item.user.id + '"  class="layui-nav-img user-photo" style="width: 32px;height: 32px;">';
                html += '<div style="font-size: 12px;">' + item.user.name + '</div>';
                html += '</td>';
                html += '<td style="background-color: #fafafa;min-height: 47px;width: 100%;padding: 5px 0;border-bottom: 1px solid #ddd;">';
                html += '<div style="font-size: 10px;">';
                html += workUtils.toDate(item.created, 'yyyy-MM-dd hh');
                html += '</div>';
                html += '<div style="word-break: break-all;white-space: normal;">';
                html += item.content;
                html += '</div>';
                html += '<div id="weekly-' + item.id + '"></div>';
                html += "</td>";
                html += "</table>";
                $("#table-weekly").append(html);
                buildWeeklyItem(item.id);
            }
            if(data.length==0){
                var html = '<div style="text-align: center;">无数据</div>';
                $("#table-weekly").append(html);
            }
        });
    }

    function buildWeeklyItem(id) {
        workUtils.ajaxGet("../report/listWeeklyItem", {
            weeklyId: id
        }, function (data) {
            var html = '';
            for (var i = 0; i < data.length; i++) {
                var item = data[i];
                html += '<div style="font-size: 12px;background-color: #eee;padding: 5px 2px;">' + item.user.name + ' ' + item.content + '</div>';
            }
            $("#weekly-" + id).html(html);
        });
    }

    layui.use(['layer', 'table', 'laydate', 'form'], function () {
        var laydate = layui.laydate;
        var form = layui.form;
        layer = layui.layer;

        //常规用法
        laydate.render({
            elem: '#weeklyTime',
            type: 'date',
            format: 'yyyy-MM-dd',
            value: workUtils.getNowDate(),
            done: function (value, date, endDate) {
                loadDefaultData();
            }
        });

        loadDefaultData();
    });

</script>